#ifndef LEETCODE_CN_CPP_PROBLEMS_8_H
#define LEETCODE_CN_CPP_PROBLEMS_8_H

#include <limits>
#include <string>

using namespace std;

namespace problem_8
{

class Solution
{
public:
    int myAtoi(string str)
    {
        int strSize = str.size();
        int i = 0;
        while (i < strSize && (str[i] == ' ' || str[i] == '\n' || str[i] == '\r' || str[i] == '\t'))
            i += 1;
        if (i >= strSize)
            return 0;

        bool isSigned = false;
        if (str[i] == '+')
            i += 1;
        else if (str[i] == '-')
        {
            isSigned = true;
            i += 1;
        }
        if (i >= strSize)
            return 0;

        long long num = 0;
        while (i < strSize && str[i] >= '0' && str[i] <= '9')
        {
            num = num * 10 + str[i] - '0';
            i += 1;
            if (num > numeric_limits<int>::max())
                return isSigned ? numeric_limits<int>::min() : numeric_limits<int>::max();
        }
        if (isSigned)
            return -num;
        return num;
    }
};

} // namespace problem_8

#endif //LEETCODE_CN_CPP_PROBLEMS_8_H
